home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 15FQ7LQ (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  5.3 KB  |  181 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.undo.UndoableEdit;
  4. import java.io.Serializable;
  5. import java.util.Vector;
  6.  
  7. public final class StringContent implements AbstractDocument.Content, Serializable {
  8.    private static final char[] empty = new char[0];
  9.    private char[] data;
  10.    private int count;
  11.    transient Vector marks;
  12.  
  13.    public StringContent() {
  14.       this(10);
  15.    }
  16.  
  17.    public StringContent(int initialLength) {
  18.       if (initialLength < 1) {
  19.          initialLength = 1;
  20.       }
  21.  
  22.       this.data = new char[initialLength];
  23.       this.data[0] = '\n';
  24.       this.count = 1;
  25.    }
  26.  
  27.    public Position createPosition(int offset) throws BadLocationException {
  28.       if (this.marks == null) {
  29.          this.marks = new Vector();
  30.       }
  31.  
  32.       return new StickyPosition(this, offset);
  33.    }
  34.  
  35.    public void getChars(int where, int len, Segment chars) throws BadLocationException {
  36.       if (where + len > this.count) {
  37.          throw new BadLocationException("Invalid location", this.count);
  38.       } else {
  39.          chars.array = this.data;
  40.          chars.offset = where;
  41.          chars.count = len;
  42.       }
  43.    }
  44.  
  45.    protected Vector getPositionsInRange(Vector v, int offset, int length) {
  46.       int n = this.marks.size();
  47.       int end = offset + length;
  48.       Vector placeIn = v == null ? new Vector() : v;
  49.  
  50.       for(int i = 0; i < n; ++i) {
  51.          PosRec mark = (PosRec)this.marks.elementAt(i);
  52.          if (mark.unused) {
  53.             this.marks.removeElementAt(i);
  54.             --i;
  55.             --n;
  56.          } else if (mark.offset >= offset && mark.offset <= end) {
  57.             placeIn.addElement(new UndoPosRef(this, mark));
  58.          }
  59.       }
  60.  
  61.       return placeIn;
  62.    }
  63.  
  64.    public String getString(int where, int len) throws BadLocationException {
  65.       if (where + len > this.count) {
  66.          throw new BadLocationException("Invalid range", this.count);
  67.       } else {
  68.          return new String(this.data, where, len);
  69.       }
  70.    }
  71.  
  72.    public UndoableEdit insertString(int where, String str) throws BadLocationException {
  73.       if (where >= this.count) {
  74.          throw new BadLocationException("Invalid location", this.count);
  75.       } else {
  76.          char[] chars = str.toCharArray();
  77.          this.replace(where, 0, chars, 0, chars.length);
  78.          if (this.marks != null) {
  79.             this.updateMarksForInsert(where, str.length());
  80.          }
  81.  
  82.          return new InsertUndo(this, where, str.length());
  83.       }
  84.    }
  85.  
  86.    public int length() {
  87.       return this.count;
  88.    }
  89.  
  90.    public UndoableEdit remove(int where, int nitems) throws BadLocationException {
  91.       if (where + nitems >= this.count) {
  92.          throw new BadLocationException("Invalid range", this.count);
  93.       } else {
  94.          String removedString = this.getString(where, nitems);
  95.          UndoableEdit edit = new RemoveUndo(this, where, removedString);
  96.          this.replace(where, nitems, empty, 0, 0);
  97.          if (this.marks != null) {
  98.             this.updateMarksForRemove(where, nitems);
  99.          }
  100.  
  101.          return edit;
  102.       }
  103.    }
  104.  
  105.    void replace(int offset, int length, char[] replArray, int replOffset, int replLength) {
  106.       int delta = replLength - length;
  107.       int src = offset + length;
  108.       int nmove = this.count - src;
  109.       int dest = src + delta;
  110.       if (this.count + delta >= this.data.length) {
  111.          int newLength = Math.max(2 * this.data.length, this.count + delta);
  112.          char[] newData = new char[newLength];
  113.          System.arraycopy(this.data, 0, newData, 0, offset);
  114.          System.arraycopy(replArray, replOffset, newData, offset, replLength);
  115.          System.arraycopy(this.data, src, newData, dest, nmove);
  116.          this.data = newData;
  117.       } else {
  118.          System.arraycopy(this.data, src, this.data, dest, nmove);
  119.          System.arraycopy(replArray, replOffset, this.data, offset, replLength);
  120.       }
  121.  
  122.       this.count += delta;
  123.    }
  124.  
  125.    void resize(int ncount) {
  126.       char[] ndata = new char[ncount];
  127.       System.arraycopy(this.data, 0, ndata, 0, Math.min(ncount, this.count));
  128.       this.data = ndata;
  129.    }
  130.  
  131.    synchronized void updateMarksForInsert(int offset, int length) {
  132.       if (offset == 0) {
  133.          offset = 1;
  134.       }
  135.  
  136.       int n = this.marks.size();
  137.  
  138.       for(int i = 0; i < n; ++i) {
  139.          PosRec mark = (PosRec)this.marks.elementAt(i);
  140.          if (mark.unused) {
  141.             this.marks.removeElementAt(i);
  142.             --i;
  143.             --n;
  144.          } else if (mark.offset >= offset) {
  145.             mark.offset += length;
  146.          }
  147.       }
  148.  
  149.    }
  150.  
  151.    synchronized void updateMarksForRemove(int offset, int length) {
  152.       int n = this.marks.size();
  153.  
  154.       for(int i = 0; i < n; ++i) {
  155.          PosRec mark = (PosRec)this.marks.elementAt(i);
  156.          if (mark.unused) {
  157.             this.marks.removeElementAt(i);
  158.             --i;
  159.             --n;
  160.          } else if (mark.offset >= offset + length) {
  161.             mark.offset -= length;
  162.          } else if (mark.offset >= offset) {
  163.             mark.offset = offset;
  164.          }
  165.       }
  166.  
  167.    }
  168.  
  169.    protected void updateUndoPositions(Vector positions) {
  170.       for(int counter = positions.size() - 1; counter >= 0; --counter) {
  171.          UndoPosRef ref = (UndoPosRef)positions.elementAt(counter);
  172.          if (ref.rec.unused) {
  173.             positions.removeElementAt(counter);
  174.          } else {
  175.             ref.resetLocation();
  176.          }
  177.       }
  178.  
  179.    }
  180. }
  181.